/**
 * 
 */
package com.yi.resources.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.yi.model.internal.IBussModelApi;
import com.yi.model.internal.IModelUIConfigApi;
import com.yi.model.objects.BussModel;
import com.yi.model.objects.DBDefinitions;
import com.yi.model.objects.Model;
import com.yi.model.objects.ModelUIConfig;
import com.yi.role.internal.IRoleApi;
import com.yi.user.internal.IUserApi;

/**
 * @author yilo
 *
 */

@Path("model")
@Scope("request")
public class ModelResource {

	private IBussModelApi bussModelApi;
	private IRoleApi roleApi;
	private IUserApi userApi;
	private IModelUIConfigApi modelUIConfigApi;
	
	@POST
	@Path("create/{modelType}")
	@Produces(MediaType.APPLICATION_JSON)
	public String create(@Context final HttpServletRequest request,
			@PathParam("modelType") final String modelType,			
			@FormParam("fields") final String fields){
		BussModel bussModel = new BussModel();
		return getBussModelApi().create(modelType, bussModel);
	}


	/**
	 * use model coll name and the modeltypeid to search model
	 * @param request
	 * @param modelid
	 * @return
	 */
	@Path("/all/{modelcollname}/{pageNum}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Object getModelTypeById(@Context final HttpServletRequest request,
												@PathParam("modelcollname") final String modelCollName,
												@PathParam("pageNum") final String pageNum){
		
		Map<String,Object> resp = new HashMap<String,Object>();
				
		if(DBDefinitions.roleColl.equals(modelCollName + "Coll")){
			//
			resp.put("head", getModelUIConfigApi().viewByName(modelCollName).fields);
			if(StringUtils.hasLength(pageNum))
				resp.put("body", getRoleApi().list(Integer.parseInt(pageNum)));
			else
				resp.put("body", getRoleApi().list(0));
			
		}else if(DBDefinitions.userColl.equals(modelCollName + "Coll")){
			resp.put("head", getModelUIConfigApi().viewByName(modelCollName).fields);
			if(StringUtils.hasLength(pageNum))
				resp.put("body", getUserApi().list(Integer.parseInt(pageNum)));
			else
				resp.put("body", getUserApi().list(0));
		}else{
			resp.put("head", getModelUIConfigApi().viewByName(modelCollName).fields);
			if(StringUtils.hasLength(pageNum))
				resp.put("body", getBussModelApi().list(modelCollName + "Coll",Integer.parseInt(pageNum)));
			else
				resp.put("body", getBussModelApi().list(modelCollName + "Coll",0));
		}
				
//		List<BussModel> bussModels = bussModelApi.list(modelname + "Coll",pageNum);
		
		return resp;
		
	}
	
	public IBussModelApi getBussModelApi() {
		return bussModelApi;
	}



	public void setBussModelApi(IBussModelApi bussModelApi) {
		this.bussModelApi = bussModelApi;
	}


	public IRoleApi getRoleApi() {
		return roleApi;
	}


	public void setRoleApi(IRoleApi roleApi) {
		this.roleApi = roleApi;
	}


	public IUserApi getUserApi() {
		return userApi;
	}


	public void setUserApi(IUserApi userApi) {
		this.userApi = userApi;
	}


	public IModelUIConfigApi getModelUIConfigApi() {
		return modelUIConfigApi;
	}


	public void setModelUIConfigApi(IModelUIConfigApi modelUIConfigApi) {
		this.modelUIConfigApi = modelUIConfigApi;
	}


	

	
}
